//获取应用实例
const app = getApp();
var api = require('../../utils/api.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenOrderM: false,
    businessCol: false,
    buttonClicked: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.code) {
      this.setApiCouponsCodeDetails(options.code, options.type);
    }
    this.showAction();
  },

  onShow: function() {
    this.setData({
      redirect_appid: app.globalData.redirect_appid, //判断是否用户版跳转过来的
    })
  },

  /**
   * 查看权限
   */
  checkAuthority: function(e) {
    util.buttonClicked(this);
    var that = this;
    var types = e.currentTarget.dataset.types;
    api.POST({
      url: app.globalData.base_url + 'shop/checkAuth',
      params: {},
      success: function(res) {
        if (res.data.code == '') {
          var is_open = res.data.data.is_open;
          var discount = res.data.data.discount;
          //检测权限
          switch (types) {
            case '1':
              if (is_open == 2) {
                that.coupons_check();
              } else {
                util.showStatus("暂无权限");
              }
              break;
            case '2':
              if (discount == 2) {
                that.order_pay_clicked();
              } else {
                util.showStatus("暂无权限");
              }
              break;
            case '3':
              if (is_open == 2) {
                that.order_record_clicked();
              } else {
                util.showStatus("暂无权限");
              }
              break;
            case '4':
              that.ticket_verification();
              break;
          }
        }
      }
    });
  },

  //显示用户可用功能
  showAction: function() {
    var accountInfo = app.globalData.accountInfo;
    this.setData({
      userInfo: accountInfo
    })
    // type 3：是商家   4：服务人员账号   |   shop_type_id 1：餐饮店   2：烟酒店
    // 分润表  服务员管理 只有商家有
    if (accountInfo.type == '4') {
      this.setData({
        businessCol: accountInfo.is_open == '1' ? true : false,
        hiddenOrderM: accountInfo.discount == "1" ? true : false
      })
    }
  },

  //商家购酒
  order_pay_clicked: function() {
    wx.navigateTo({
      url: '../classification/classification',
    })
  },

  //订单列表
  order_record_clicked: function() {
    wx.navigateTo({
      url: '../orderRecored/orderRecored',
    })
  },

  //邀请代理商
  // inviteAgent: function(){
  //   wx.navigateTo({
  //     url: '/pages/inviteAgent/inviteAgent',
  //   })
  // },

  //扫码取酒
  coupons_check: function() {
    var that = this;
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        var rest = res.result;
        if (rest.indexOf("code") < 0 || rest.indexOf("type") < 0) {
          util.showStatus("请扫描核销二维码");
          return;
        }
        var arr = res.result.split("?");
        var str1 = arr[1];
        var atrr1 = str1.split("&")[0];
        var code = atrr1.split("=")[1];
        var types = str1.split("&")[1].split("=")[1];
        that.setApiCouponsCodeDetails(code, types);
      }
    });

  },

  //餐券核销
  ticket_verification: function() {
    var that = this;
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        var rest = res.result;
        if (rest.indexOf("code") < 0 || rest.indexOf("ticketVerification") < 0) {
          util.showStatus("请扫描餐券二维码");
          return;
        }
        var arr = res.result.split("?");
        var str1 = arr[1];
        var code = str1.split("=")[1];
        wx.navigateTo({
          url: '/pages/mealTicketVerification/mealTicketVerification?code=' + code,
        })
      }
    });

  },

  //请求后台，改变订单状态为已扫码
  setApiCouponsCodeDetails: function(couponsCode, couponsType) {
    var params = {
      code: couponsCode,
      type: couponsType
    }
    util.showLoading("加载中...");
    api.POST({
      url: app.globalData.base_url + 'shop/scanInfo',
      params: params,
      success: function(res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          var oid = res.data.data.oid
          wx.navigateTo({
            url: '../orderVerification/orderVerification?oid=' + oid,
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            showCancel: false,
            confirmText: '知道了'
          })
        }
      },
      fail: function(res) {
        wx.hideLoading();
        util.showError("网络错误，请检查网络。");
      }
    });
  },

  //去酒掌柜用户版
  jiuzhanggui: function() {
    wx.navigateToMiniProgram({
      appId: 'wx2f5360b509c07b8a',
      path: 'pages/index/index',
      envVersion: 'trial', //develop	开发版 trial	体验版  release	正式版
    })
  },

  onShareAppMessage: function(res) {
    var that = this;
    var shop_name = app.globalData.accountInfo.username;
    var shop_mobile = app.globalData.accountInfo.mobile;
    var url = '/pages/inviteAgent/inviteAgent?shop_name=' + shop_name + '&shop_mobile=' + shop_mobile;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: shop_name + '邀请您成为Ta的代理商',
        path: url,
        imageUrl: '/images/bg_inviteAgent_share.jpg',
        success: function(res) {
          //转发成功
        },
        fail: function(res) {
          // 转发失败
        }
      }
    }
  }

})